set @@session.sql_log_bin=0;

-- 0、 create root@'127.0.0.1'
create user root@'127.0.0.1' identified by '{{mysql_root_pwd}}';
grant all on *.* to root@'127.0.0.1' with grant option;

alter user root@'localhost' identified by '{{mysql_root_pwd}}';

-- 1、 create monitor user
create user {{mysql_monitor_user}}@'127.0.0.1' identified by '{{mysql_monitor_pwd}}';
grant replication client on *.* to {{mysql_monitor_user}}@'127.0.0.1';
grant process on *.* to {{mysql_monitor_user}}@'127.0.0.1';
grant select on performance_schema.* to {{mysql_monitor_user}}@'127.0.0.1';
grant select on sys.* to {{mysql_monitor_user}}@'127.0.0.1';

create user {{mysql_monitor_user}}@'localhost' identified by '{{mysql_monitor_pwd}}';
grant replication client on *.* to {{mysql_monitor_user}}@'localhost';
grant process on *.* to {{mysql_monitor_user}}@'localhost';
grant select on performance_schema.* to {{mysql_monitor_user}}@'localhost';
grant select on sys.* to {{mysql_monitor_user}}@'localhost';

-- 2、create dumper user for mysqldump backup
create user {{mysql_dumper_user}}@'127.0.0.1' identified by '{{mysql_dumper_pwd}}';
grant reload,super,replication client on *.* to {{mysql_dumper_user}}@'127.0.0.1';
grant select,event on *.* to {{mysql_dumper_user}}@'127.0.0.1';
grant show view on *.* to {{mysql_dumper_user}}@'127.0.0.1';
grant lock tables on *.* to {{mysql_dumper_user}}@'127.0.0.1';
grant trigger on *.* to {{mysql_dumper_user}}@'127.0.0.1';

create user {{mysql_dumper_user}}@'localhost' identified by '{{mysql_dumper_pwd}}';
grant reload,super,replication client on *.* to {{mysql_dumper_user}}@'localhost';
grant select,event on *.* to {{mysql_dumper_user}}@'localhost';
grant show view on *.* to {{mysql_dumper_user}}@'localhost';
grant lock tables on *.* to {{mysql_dumper_user}}@'localhost';
grant trigger on *.* to {{mysql_dumper_user}}@'localhost';

-- 3、create extra user for extrabackup
create user {{mysql_extra_user}}@'127.0.0.1' identified by '{{mysql_extra_pwd}}';
grant reload,lock tables on *.* to {{mysql_extra_user}}@'127.0.0.1';
grant replication client on *.* to {{mysql_extra_user}}@'127.0.0.1';
grant create tablespace  on *.* to {{mysql_extra_user}}@'127.0.0.1';
grant process            on *.* to {{mysql_extra_user}}@'127.0.0.1';
grant super              on *.* to {{mysql_extra_user}}@'127.0.0.1';
grant create,insert,select      on percona_schema.xtrabackup_history to {{mysql_extra_user}}@'127.0.0.1';


create user {{mysql_extra_user}}@'localhost' identified by '{{mysql_extra_pwd}}';
grant reload,lock tables on *.* to {{mysql_extra_user}}@'localhost';
grant replication client on *.* to {{mysql_extra_user}}@'localhost';
grant create tablespace  on *.* to {{mysql_extra_user}}@'localhost';
grant process            on *.* to {{mysql_extra_user}}@'localhost';
grant super              on *.* to {{mysql_extra_user}}@'localhost';
grant create,insert,select      on percona_schema.xtrabackup_history to {{mysql_extra_user}}@'localhost';

{% if 'mysql-8.0' in mysql_version %}
create user {{mysql_clone_user}}@'127.0.0.1' identified by '{{mysql_clone_pwd}}';
grant backup_admin on *.* to {{mysql_clone_user}}@'127.0.0.1';
grant clone_admin on *.* to {{mysql_clone_user}}@'127.0.0.1';
{% endif%}

set @@session.sql_log_bin=1;